<template>
  <view class="main" v-if="goods_detail.goods_price">
    <view class="swiper">
      <swiper class="swiper-box" :circular="true" :indicator-dots="true" :autoplay="true" :interval="2000"
        :duration="1000">
        <swiper-item class="swiper-box-item" v-for="(item,index) in goods_detail.pics" :key="index">
          <image @click="previewImage(index)" :src="item.pics_big_url" mode="heightFix"></image>
        </swiper-item>
      </swiper>
    </view>
    <view class="text">
      <view class="price">
        ￥{{goods_detail.goods_price}}
      </view>
      <view class="name">
        <view class="name-left">
          {{goods_detail.goods_name}}
        </view>
        <view class="name-right">
          <uni-icons type="heart" size="20"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="express">
        快递：免运费
      </view>
    </view>
    <view class="introduce">
      <rich-text :nodes="goods_detail.goods_introduce"></rich-text>
    </view>
    <view class="fixed">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="click"
        @buttonClick="buttonClick"></uni-goods-nav>
    </view>
  </view>
</template>

<script>
  import {mapMutations,mapGetters} from 'vuex'
  export default {
    computed:{
      ...mapGetters('cart',['changeInfo'])
    },
    watch:{
      changeInfo:{
        handler(newVal){
        // console.log(newVal);
        this.options[1].info = newVal
      },
      deep: true,
      immediate: true
      }
    },
    data() {
      return {
        goods_id: '',
        goods_detail: {},
        options: [{
            icon: 'shop',
            text: '店铺'
          },
          {
            icon: 'cart',
            text: '购物车',
            info: 0
          }
        ],
        buttonGroup: [{
          text: '加入购物车',
          backgroundColor: '#c00000',
          color: '#fff'
        }, {
          text: '加入购物车',
          backgroundColor: '#ffa200',
          color: '#fff'
        }]
      }
    },
    onLoad(options) {
      this.goods_id = options.goods_id
      this.getData()
    },
    methods: {
      ...mapMutations({addcateList:'cart/addcateList'}),
      click(e) {
        console.log(e);
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }

      },
      buttonClick(e) {
        // console.log('buttonClick')
        if(e.content.text === '加入购物车'){
          const addCate = {
            goods_count:1,
            goods_status:true,
            goods_id:this.goods_detail.goods_id,
            goods_price:this.goods_detail.goods_price,
            goods_name:this.goods_detail.goods_name,
            goods_small_logo:this.goods_detail.goods_small_logo,
          }
          this.addcateList(addCate)
          uni.showToast({
            title:'添加成功'
          })
        }
      },
      previewImage(index) {
        // 获取轮播图数组
        const res = this.goods_detail.pics.map(item => {
          return item.pics_big_url
        })
        uni.previewImage({
          current: index,
          urls: res
        })
      },
      async getData() {
        const {
          data: res
        } = await uni.$http.get(`/api/public/v1/goods/detail?goods_id=${this.goods_id}`).catch(err => err)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.goods_detail = res.message
        this.goods_detail.goods_introduce = this.goods_detail.goods_introduce.replace(/<img/g,
          '<img style="display: block;" ').replace(/webp/g, 'jpg')

      }
    }
  }
</script>

<style lang="scss">
  .main {
    position: relative;
    padding-bottom: 100rpx;

    .swiper {
      &-box {
        height: 750rpx;

        &-item {
          image {
            height: 750rpx;
          }
        }
      }
    }

    .text {
      padding: 20rpx;

      .price {
        color: #c00000;
        margin: 20rpx 0;
        font-size: 20px;
      }

      .name {
        display: flex;
        font-size: 14px;

        &-left {
          word-break: break-all;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
          margin-right: 30rpx;
        }

        &-right {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          border-left: 1px solid #ccc;
          width: 200rpx;
        }
      }

      .express {
        color: #b4b4b4;
        font-size: 14px;
        margin-top: 20rpx;
      }
    }

    .fixed {
      width: 100%;
      position: fixed;
      bottom: 0;
      left: 0;
    }
  }
</style>
